#!/bin/bash

# DeepSeek API配置脚本
# 用于设置DeepSeek R1模型的API密钥

echo "🚀 QuoteHub Stocks - DeepSeek R1 配置向导"
echo "=========================================="
echo ""

# 检查是否已经设置了API密钥
if [ -n "$DEEPSEEK_API_KEY" ]; then
    echo "✅ 检测到现有的DeepSeek API密钥"
    echo "当前密钥: ${DEEPSEEK_API_KEY:0:8}..."
    echo ""
    read -p "是否要更新API密钥? (y/N): " update_key
    if [[ ! "$update_key" =~ ^[Yy]$ ]]; then
        echo "保持现有配置"
        exit 0
    fi
fi

echo "📝 请输入您的DeepSeek API密钥:"
echo "   - 访问 https://platform.deepseek.com 获取API密钥"
echo "   - API密钥格式: sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
echo ""

read -p "API密钥: " api_key

# 验证API密钥格式
if [[ ! "$api_key" =~ ^sk-.{32,}$ ]]; then
    echo "❌ API密钥格式不正确，请检查后重试"
    exit 1
fi

# 设置环境变量
export DEEPSEEK_API_KEY="$api_key"

# 写入到.env文件
echo "DEEPSEEK_API_KEY=$api_key" > .env
echo "✅ API密钥已保存到 .env 文件"

# 写入到shell配置文件
shell_config=""
if [ -n "$ZSH_VERSION" ]; then
    shell_config="$HOME/.zshrc"
elif [ -n "$BASH_VERSION" ]; then
    shell_config="$HOME/.bashrc"
fi

if [ -n "$shell_config" ]; then
    echo "" >> "$shell_config"
    echo "# DeepSeek API Key for QuoteHub Stocks" >> "$shell_config"
    echo "export DEEPSEEK_API_KEY=\"$api_key\"" >> "$shell_config"
    echo "✅ API密钥已添加到 $shell_config"
fi

echo ""
echo "🎉 DeepSeek R1配置完成！"
echo ""
echo "现在您可以使用以下命令启动AI选股器:"
echo "   python main.py ai-v2                    # 启用R1深度分析"
echo "   python main.py ai-v2 --strategy growth  # 成长策略 + R1分析"
echo "   python main.py ai-v2 --test             # 测试R1连接状态"
echo ""
echo "💡 提示: 重新启动终端或运行 'source ~/.zshrc' 使环境变量生效" 